﻿using System;
using System.Collections.Generic;
using System.Data;
using dtSearch.Engine;
using SearchEngine;

namespace DTSearchHelper
{
    public class DocumentsHelper :ISearchEngineDocuments,IDisposable
    {

        public DocumentsHelper(Exception []errors,SearchJob searchjobObject, DateTime startTime, DateTime endTime)
        {
            this.errors = errors;
            this.searchjobObject = searchjobObject;
            this.preparedSearchStatement = this.searchjobObject.Request;
            this.startTime = startTime;
            this.endTime = endTime;
        }

        private SearchJob searchjobObject = null;
        public SearchJob SearchjobObject
        {
            get { return searchjobObject; }
        }

        Exception [] errors=null;

        public Exception[] Errors
        {
            get { return errors; }
        }

        private string preparedSearchStatement = "";
        public string PreparedSearchStatement
        {
            get { return preparedSearchStatement; }
        }


        //get count
        public int Count
        {
            get
            {
                int count = 0;
                if (this.searchjobObject != null)
                {
                    count = searchjobObject.Results.Count;
                }
                return count;
            }
        }

        private DateTime startTime;
        public DateTime StartTime
        {
            get { return startTime; }
        }
        
        private DateTime endTime;
        public DateTime EndTime
        {
            get { return endTime; }
        }
        
        public TimeSpan SearchTime
        {
            get
            {
                return endTime - startTime;
            }
        }
        
        //get field at specified row
        public string this[int RowNumber,string FieldName]
        {
            get
            {
                this.searchjobObject.Results.GetNthDoc(RowNumber);
                string m = "";
                if (this.searchjobObject != null)
                {
                    m = this.searchjobObject.Results.get_DocDetailItem(FieldName.Trim()).Trim();
                    m = DocumentsHelper.RemoveStatrAndEnd(m);
                }
                return m.Trim();
            }
        }
        
        //remove statrt and end from field
        public static string RemoveStatrAndEnd(string fieldValue)
        {
            string s = fieldValue;
            List<string> list = new List<string>();
            list.Add("dabs:");
            list.Add("daut:");
            list.Add("dcap:");
            list.Add("dcnr:");
            list.Add("ddate:");
            list.Add("dds:");
            list.Add("did:");
            list.Add("ditype:");
            list.Add("dkey:");
            list.Add("dpkey:");
            list.Add("dnat:");
            list.Add("dorg:");
            list.Add("dpage:");
            list.Add("dper:");
            list.Add("dpl:");
            list.Add("dsr:");
            list.Add("dsrcode:");
            list.Add("dsub:");
            list.Add("dtyp:");
            list.Add("dtypeid:");
            list.Add("imagen:");
            list.Add("xabs:");
            list.Add("xaut:");
            list.Add("xautcomp:");
            list.Add("xcap:");
            list.Add("xcnr:");
            list.Add("xkey:");
            list.Add("xnat");
            list.Add("xorg:");
            list.Add("xorgcomp:");
            list.Add("xper:");
            list.Add("xpercomp:");
            list.Add("xpl:");
            list.Add("xplcomp:");
            list.Add("xsr:");
            list.Add("xsub:");
            list.Add("dfulltxt:");
            list.Add("dpageno:");
            list.Add("dpagefrom:");
            list.Add("dpageto:");
            for (int i = 0; i < list.Count; i++)
            {
                if (fieldValue.ToLower().StartsWith(list[i]))
                {
                    try
                    {
                        s = fieldValue.Substring(fieldValue.IndexOf(" "), fieldValue.LastIndexOf(" ") - fieldValue.IndexOf(" ")).Trim();
                    }
                    catch
                    {
                        s = fieldValue;
                    }
                    break;
                }
            }
            return s;
        }

        
        public DataTable ExtractDataTable(int start, int end, params string[] fields)
        {
            DataTable dt = null;
            if (this.searchjobObject != null && start < this.Count && end < this.Count)
            {
                dt = fillTableWithFieldsAndValues(start, end, fields);
            }
            return dt;
        }
        //===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================--
        
        private DataTable fillTableWithFieldsAndValues(int start, int end, params string[] fields)
        {
            List<string> totalFields = new List<string>();
            //if (this.showDTSearchFields)
            //{
            //    totalFields.AddRange(SearchHelper.getConstantsDTSearchFields());
            //}
            if (fields != null && fields.Length > 0)
            {
                totalFields.AddRange(fields);
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("IndexDateTime",typeof(DateTime));
            for (int i = start; i < end + 1; i++)
            {
                this.searchjobObject.Results.GetNthDoc(i);
                if (fields == null || fields.Length == 0)
                {
                    foreach (string key in this.searchjobObject.Results.CurrentItem.UserFields.Keys)
                    {
                        if (!totalFields.Contains(key))
                        {
                            totalFields.Add(key);
                        }
                    }
                }

                DataRow dr = dt.NewRow();
                for (int j = 0; j < totalFields.Count; j++)
                {
                    String fieldName = totalFields[j];

                    if (!dt.Columns.Contains(fieldName))
                    {
                        dt.Columns.Add(fieldName);
                    }

                    string value = this.searchjobObject.Results.get_DocDetailItem(fieldName);
                    value = DocumentsHelper.RemoveStatrAndEnd(value);

                    dr[fieldName] = value;
                }
                dr["IndexDateTime"]=this.searchjobObject.Results.CurrentItem.ModifiedDate;
                dt.Rows.Add(dr);
            }
            return dt;
        }
        //===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================--




        #region IDisposable Members

        public void Dispose()
        {
            this.searchjobObject.Results.Dispose();
            this.searchjobObject.Dispose();
        }

        #endregion
    }
}
